<script setup lang="ts">
import { ref } from "vue";
import { useDept } from "./hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import DeptForm from "./deptForm.vue";

import Delete from "@iconify-icons/ep/delete";
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";

defineOptions({
  name: "Dept"
});

const clickAddDeptBut = () => {
  deptFormRef.value.showdialogForAddDept(depList.value, "添加部门");
};

const formRef = ref();
const tableRef = ref();
const {
  deptFormRef,
  form,
  loading,
  columns,
  depList,
  dataList,
  onSearch,
  resetForm,
  handleDelete,
  handleSelectionChange
} = useDept();
</script>

<template>
  <div>
    <DeptForm ref="deptFormRef" @onSearchEnv="onSearch" />
    <div class="main">
      <el-form
        ref="formRef"
        :inline="true"
        :model="form"
        class="bg-bg_color w-[99/100] pl-8 pt-4"
      >
        <el-form-item label="部门名称：" prop="user">
          <el-input
            v-model="form.user"
            placeholder="请输入部门名称"
            clearable
            class="!w-[200px]"
          />
        </el-form-item>
        <el-form-item label="状态：" prop="status">
          <el-select
            v-model="form.status"
            placeholder="请选择状态"
            clearable
            class="!w-[180px]"
          >
            <el-option label="开启" value="1" />
            <el-option label="关闭" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :icon="useRenderIcon(Search)"
            :loading="loading"
            @click="onSearch"
          >
            搜索
          </el-button>
          <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <PureTableBar
        title="部门列表"
        :tableRef="tableRef?.getTableRef()"
        @refresh="onSearch"
      >
        <template #buttons>
          <el-button
            type="primary"
            @click="clickAddDeptBut"
            :icon="useRenderIcon(AddFill)"
          >
            新增部门
          </el-button>
        </template>
        <template v-slot="{ size, checkList }">
          <pure-table
            ref="tableRef"
            border
            align-whole="center"
            row-key="id"
            showOverflowTooltip
            table-layout="auto"
            default-expand-all
            :loading="loading"
            :size="size"
            :data="dataList"
            :columns="columns"
            :checkList="checkList"
            :header-cell-style="{
              background: 'var(--el-table-row-hover-bg-color)',
              color: 'var(--el-text-color-primary)'
            }"
            @selection-change="handleSelectionChange"
          >
            <template #operation="{ row }">
              <el-popconfirm @confirm="handleDelete(row)" title="是否确认删除?">
                <template #reference>
                  <el-button
                    class="reset-margin"
                    link
                    type="primary"
                    :size="size"
                    :icon="useRenderIcon(Delete)"
                  >
                    删除
                  </el-button>
                </template>
              </el-popconfirm>
            </template>
          </pure-table>
        </template>
      </PureTableBar>
    </div>
  </div>
</template>
